package nacos

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/gobase/getfrom"
	"gitee.com/ymofen/refobjects"
)

func init() {
	// nacos://servers=58.49.94.131:18351:18352&namespace=&group=DEV&dataId=yangmf
	getfrom.Register("nacos", func(args ...interface{}) (data []byte, err error) {
		conf := args[0].(string)
		confMap := gobase.NewStrMap()
		confMap.URLFormDecode(conf)

		connMap := gobase.NewStrMap()
		connMap.URLFormDecode(conf)
		connMap.DeleteByKeylst("dataId", "group")
		connMap["type"] = "nacosCfgClient"
		connConf := connMap.URLEncode0()
		ref, err := refobjects.Acquire(connConf)
		if err != nil {
			return nil, err
		}
		if ref.RefCounter() > 1 { // nacos 释放有bug, 不停创建会有问题
			defer ref.Close()
		}

		clt := ref.Object.(*NacosConfigClient)
		str, err := clt.GetConfig(confMap["dataId"], confMap["group"])
		if err != nil {
			return nil, err
		}
		return []byte(str), nil
	})

}
